/**
 * ZhiZun.com Inc.
 * Copyright (c) 2004-2010 All Rights Reserved.
 */
package com.zhz.lib.project.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.zhz.lib.project.hibernate.Feedbacklib;
import com.zhz.lib.project.service.feedback.FeedbackService;
import com.zhz.project.common.util.alibaba.StringUtil;
import com.zhz.project.common.util.ibatispage.IbatisPage;
import com.zhz.project.common.util.zhzutil.ZhzCommonUtil;
import com.zhz.project.common.util.zhzutil.ZhzHttpRequestUtil;

/**
 * 
 * @author liwei2672
 * @version $Id: FeedbackController.java, v 0.1 2010-7-6 下午10:43:52 liwei2672
 *          Exp $
 */
@Controller
@RequestMapping("/feedback.htm")
public class FeedbackController {
	private static Logger logger = Logger.getLogger(NewsController.class);

	@Autowired
	private FeedbackService feedbackService;
	@Autowired
	private IbatisPage ibatisPage;

	private static String pageSql = "selectAllFeedbacklibByPage";
	private static String countSql = "selectAllFeedbacklibCount";

	@RequestMapping(method = RequestMethod.GET)
	public String get(ModelMap model, HttpServletRequest request,
			HttpServletResponse response) {

		try {
			// 传递给Ibatis的map
			Map map = new HashMap();

			// 当前页
			String indexPage = ZhzHttpRequestUtil.getParameters(request,
					"indexPage");
			Integer pageNo = null;
			if (StringUtil.isNotBlank(indexPage)) {
				pageNo = new Integer(indexPage);
			}
			map.put("indexPage", pageNo);

			ZhzHttpRequestUtil.writeMapToModelMap(model, ibatisPage.getPage(
					map, pageSql, countSql));

			// 将News转换为NewsVO
			List<Feedbacklib> feedbacklibList = (List<Feedbacklib>) model
					.get("list");

			model.put("list", feedbacklibList);

			return "feedback";
		} catch (Exception e) {
			logger.error(e.toString());
			return null;
		}

	}

	@RequestMapping(method = RequestMethod.POST)
	public String thanks(ModelMap model, HttpServletRequest request,
			HttpServletResponse response) {
		String Name = ZhzHttpRequestUtil.getParameters(request, "Name");
		String Email = ZhzHttpRequestUtil.getParameters(request, "Email");
		String inputRand = ZhzHttpRequestUtil.getParameters(request,
				"inputRand");
		String Comment = ZhzHttpRequestUtil.getParameters(request, "Comment");
		String rand = (String) request.getSession().getAttribute("rand");

		if (StringUtil.equals(inputRand, rand)) {
			Feedbacklib feedbacklib = new Feedbacklib();
			feedbacklib.setName(Name);
			feedbacklib.setEmail(Email);
			feedbacklib.setComments(Comment);
			feedbacklib.setCommentsdate(new Date());
			feedbacklib.setIp(ZhzCommonUtil.getIpAddr(request));
			feedbacklib.setAproveState(0);
			feedbacklib.setIfDelete(0);

			if (feedbackService.saveFeedback(feedbacklib)) {
				model.put("result", "添加记录成功!");
			} else {
				model.put("result", "添加记录失败!");
				model.put("Name", Name);
				model.put("Email", Email);
				model.put("Comment", Comment);
			}
		} else {
			model.put("result", "添加记录失败!您的验证码输入错误。");
			model.put("Name", Name);
			model.put("Email", Email);
			model.put("Comment", Comment);
		}

		return get(model, request, response);
	}

}
